﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     filelistwidget.cpp
// 描述:
// 作者:      颜磊
// 创建工程：   AudioComment
// 创建时间:   2024-06-25 14:12
///////////////////////////////////////////////////////////////////////////
#include "filelistwidget.h"
#include "ui_filelistwidget.h"

#include <QFileDialog>
#include <QSettings>

FileListWidget::FileListWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FileListWidget)
{
    ui->setupUi(this);
    initParam();
    tableModel = new QStandardItemModel();
    tableModel->setHorizontalHeaderItem(0,new QStandardItem(QString("文件")));
    tableModel->setHorizontalHeaderItem(1,new QStandardItem(QString("路径")));
    ui->fileTableView->setModel(tableModel);
    ui->fileTableView->setSelectionBehavior(QAbstractItemView::SelectRows);//单行选中
    ui->fileTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置单元格不可编辑
    ui->fileTableView->verticalHeader()->hide();
    ui->fileTableView->hideColumn(1);
    ui->fileTableView->horizontalHeader()->setStretchLastSection(true);

    qRegisterMetaType<QList<YFileListInfo>>("QList<YFileListInfo>");
    connect(&fileListThread,&FileListThread::signal_clearList,this,&FileListWidget::slot_clearList);
    connect(&fileListThread,&FileListThread::signal_fileList,this,&FileListWidget::slot_fileList);

    fileListThread.setDirPath(ui->dirPath->text());
    fileListThread.setFilter(ui->nameFilter->text());
    fileListThread.start();
}

FileListWidget::~FileListWidget()
{
    fileListThread.stop();
    fileListThread.quit();
    fileListThread.wait();
    seriParam();
    delete ui;
}

void FileListWidget::slot_clearList()
{
    tableModel->removeRows(0,tableModel->rowCount());
}

void FileListWidget::slot_fileList(QList<YFileListInfo> infos)
{
    QIcon icon;
    for (YFileListInfo &info:infos) {
        bool exsit = false;
        for (int i=0;i<tableModel->rowCount();i++) {
            if(tableModel->index(i,0).data().toString() == QString(info.fileName)){
                exsit = true;
                tableModel->setItem(i,0,new QStandardItem(info.marked ? QIcon(":/public_res/ico/online.ico"): QIcon(":/public_res/ico/offline.ico"),QString(info.fileName)));
                tableModel->setItem(i,1,new QStandardItem(QString(info.filePath)));
            }
        }
        if(!exsit){
            tableModel->insertRow(tableModel->rowCount());
            int inserRow = tableModel->rowCount() -1;
            tableModel->setItem(inserRow,0,new QStandardItem(info.marked ? QIcon(":/public_res/ico/online.ico"): QIcon(":/public_res/ico/offline.ico"),QString(info.fileName)));
            tableModel->setItem(inserRow,1,new QStandardItem(QString(info.filePath)));
        }
    }
}

void FileListWidget::on_selDirBtn_clicked()
{
    QString dirPath = QFileDialog::getExistingDirectory(this,"选择文件夹",ui->dirPath->text());
    if(!dirPath.isEmpty()){
        ui->dirPath->setText(dirPath);
        fileListThread.setDirPath(ui->dirPath->text());
    }
}

void FileListWidget::on_fileTableView_doubleClicked(const QModelIndex &index)
{
    signal_openDisposeWidget(tableModel->index(index.row(),1).data().toString());
}

void FileListWidget::initParam()
{
    QSettings setting(QApplication::applicationDirPath()+"/config/application.ini");
    setting.beginGroup("fileList");
    ui->dirPath->setText(setting.value("dirPath").toString());
     ui->nameFilter->setText(setting.value("nameFilter").toString());
}

void FileListWidget::seriParam()
{
    QSettings setting(QApplication::applicationDirPath()+"/config/application.ini");
    setting.beginGroup("fileList");
    setting.setValue("dirPath",ui->dirPath->text());
    setting.setValue("nameFilter",ui->nameFilter->text());
}

void FileListWidget::on_nameFilter_editingFinished()
{
    fileListThread.setFilter(ui->nameFilter->text());
}
